/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package config;


import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Priority;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;



/**
 *
 * @author freixo
 */
public class Inicializador {
    
    public static void cargarConfiguracion(){

        
        
        
        Properties p= new Properties();
        try {
            URL url = ClassLoader.getSystemResource("log4j.properties");
            PropertyConfigurator.configure(url);
            
            url = ClassLoader.getSystemResource("config/app-config.properties");
            p.load(url.openStream());
        } catch (IOException ex) {
            ex.printStackTrace();
            Logger.getLogger(Inicializador.class.getName()).log(Priority.FATAL, null, ex);
        }
                
        for(Field f :Constantes.class.getDeclaredFields()){
            if(p.get(f.getName())!=null){
                try {
                    Constantes.class.getDeclaredField(f.getName()).set(f.getType(), p.get(f.getName()));
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(Inicializador.class.getName()).log(Priority.FATAL, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Inicializador.class.getName()).log(Priority.FATAL, null, ex);
                } catch (NoSuchFieldException ex) {
                    Logger.getLogger(Inicializador.class.getName()).log(Priority.FATAL, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(Inicializador.class.getName()).log(Priority.FATAL, null, ex);
                }
            }
        }
        
        
    }
    
    public static void crearCarpetas(){
        File f;
        f = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+Constantes.DIRECTORIO_TXT_XERADOS);
        f.mkdirs();
        f = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+Constantes.DIRECTORIO_RAIZ_INDICE);
        f.mkdirs();
    }
    
    
}
